<template>
  <div class="login-wrapper">
    <div class="login-container">
      <div class="tab-container">
        <span class="tab active">登录</span>
        <router-link to="/register">
          <span class="tab">注册</span>
        </router-link>
      </div>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <input 
            type="text" 
            v-model.trim="formData.username"
            placeholder="用户名"
            @blur="validateUsername"
            required 
          />
          <span class="error-message" v-if="errors.username">{{ errors.username }}</span>
        </div>
        <div class="form-group">
          <input 
            type="password" 
            v-model.trim="formData.password"
            placeholder="密码" 
            @blur="validatePassword"
            required
          />
          <span class="error-message" v-if="errors.password">{{ errors.password }}</span>
        </div>
        <div class="agreement">
          <label>
            <input type="checkbox" v-model="agreed">
            <span>已阅读并同意账号使用协议和隐私政策</span>
          </label>
        </div>
        <button type="submit" :disabled="!isFormValid">登录</button>
      </form>
    </div>
  </div>
</template>

<script>
import { ref, reactive, computed } from 'vue'
import { useRouter } from 'vue-router'
import { login } from '../api/api'
import useToken from '../store/token'

export default {
  setup() {
    const router = useRouter()
    const { updateToken } = useToken()
    
    const formData = reactive({
      username: '',
      password: ''
    })
    
    const errors = reactive({
      username: '',
      password: ''
    })
    
    const agreed = ref(false)

    const validateUsername = () => {
      errors.username = formData.username.length < 3 ? '用户名不能少于3个字符' : ''
    }

    const validatePassword = () => {
      errors.password = formData.password.length < 5 ? '密码不能少于5个字符' : ''
    }

    const isFormValid = computed(() => 
      formData.username.length >= 3 && 
      formData.password.length >= 5 && 
      agreed.value &&
      !errors.username &&
      !errors.password
    )

    const handleLogin = async () => {
      validateUsername()
      validatePassword()
      
      if (!agreed.value) {
        alert('请先同意用户协议')
        return
      }

      if (!isFormValid.value) return

      try {
        const response = await login({
          username: formData.username,
          password: formData.password
        })
        console.log(response);
        if (response.username) {
          updateToken(response.username)
          localStorage.setItem('userName', response.username)
          localStorage.setItem('userAvatar', response.userAvatar)
          router.push('/index')
        }
      } catch (error) {
        console.error('登录失败:', error)
        alert('登录失败，请检查用户名和密码')
      }
    }

    return {
      formData,
      agreed,
      handleLogin,
      errors,
      validateUsername,
      validatePassword,
      isFormValid
    }
  }
}
</script>

<style scoped>
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #fff;
}

.login-container {
  width: 400px;
  padding: 20px;
}

.tab-container {
  margin-bottom: 24px;
  border-bottom: 1px solid #e6e6e6;
}

.tab {
  display: inline-block;
  font-size: 24px;
  padding: 10px 0;
  margin-right: 20px;
  color: #666;
  cursor: pointer;
}

.tab.active {
  color: #ff6700;
  border-bottom: 2px solid #ff6700;
}

.form-group {
  margin-bottom: 14px;
}

input[type="text"],
input[type="password"] {
  width: 100%;
  padding: 14px;
  border: 1px solid #e6e6e6;
  border-radius: 4px;
  font-size: 14px;
  background-color: #f9f9f9;
  transition: all 0.3s ease;
}

input[type="text"]:focus,
input[type="password"]:focus {
  border-color: #ff6700;
  outline: none;
  background-color: #fff;
  box-shadow: 0 0 0 2px rgba(255, 103, 0, 0.2);
}

.agreement {
  margin: 10px 0 20px;
  font-size: 14px;
  color: #666;
}

.agreement input[type="checkbox"] {
  margin-right: 5px;
}

button {
  width: 100%;
  padding: 14px;
  background-color: #ff6700;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

button:hover {
  background-color: #f25807;
}

.error-message {
  color: #ff4d4f;
  font-size: 12px;
  margin-top: 4px;
  display: block;
}

button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

button:disabled:hover {
  background-color: #ccc;
}
</style>